package l6;

import java.util.Scanner;
import java.util.Stack;

public class match {
    public static void main(String[] args) {
        Stack<Character> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入表达式：");
        String expression = sc.nextLine();
        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else if (c == ')' || c == ']' || c == '}') {
                if (stack.isEmpty()) {
                    System.out.println("错误，多余的右括号 " + c + " ");
                }
                char top = stack.peek();
                if ((c == ')' && top == '(') ||
                        (c == ']' && top == '[') ||
                        (c == '}' && top == '{')) {
                    stack.pop();
                } else {
                    System.out.println("括号类型不匹配，" + top + " vs " + c);
                }
            }
        }
        if (stack.isEmpty()) {
            System.out.println("括号完全匹配");

        } else {
            System.out.println("错误，未闭合的左括号");
        }
    }
}
